package com.dubbo.provider.tomcat;

import org.apache.catalina.*;
import org.apache.catalina.connector.Connector;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.core.StandardEngine;
import org.apache.catalina.core.StandardHost;
import org.apache.catalina.startup.Tomcat;

/**
 * @description:
 * @author: 石亚宁
 * @create: 2020-05-28 15:27
 **/

public class TomcatServer {
	public void start(String hostName,int port) throws LifecycleException {
		Tomcat tomcat=new Tomcat();
		Server server = tomcat.getServer();
		Service service = server.findService("Tomcat");
		Connector connector=new Connector();
		Host host=new StandardHost();
		host.setName(hostName);
		connector.setPort(port);
		Engine engine=new StandardEngine();
		engine.setDefaultHost(hostName);
		Context context=new StandardContext();
		String contextPath="";
		context.setPath(contextPath);
		context.addLifecycleListener(new Tomcat.FixContextListener());
		host.addChild(context);
		engine.addChild(host);
		service.setContainer(engine);
		service.addConnector(connector);
		tomcat.addServlet(contextPath,"dispatcher",new DispatchServlet ());
		context.addServletMappingDecoded("/*","dispatcher");
		tomcat.start();
		System.out.println("启动地址"+hostName+"端口号："+port);
		tomcat.getServer().await();


	}
}
